package cst.neu.yuejian_31.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.List;

import cst.neu.yuejian_31.R;
import cst.neu.yuejian_31.adapter.TrainAdapter;
import cst.neu.yuejian_31.bean.TrainBean;
import cst.neu.yuejian_31.iface.TrainListener;
import cst.neu.yuejian_31.iface.TrainSearchListener;
import cst.neu.yuejian_31.model.TrainModel;
import cst.neu.yuejian_31.model.TrainSearchModel;

/**
 * A simple {@link Fragment} subclass.
 */
public class TrainFragment extends BaseFragment implements View.OnClickListener,TrainSearchListener {

    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private TrainAdapter trainAdapter;
    private List<TrainBean> list=null;
    private View view=null;
    private Context context;
    private ImageView searchTrain;
    private EditText trainName;



    TrainListener trainListener=new TrainListener() {
        @Override
        public void onResponse(List<TrainBean> listbean) {
            list=listbean;
            trainAdapter.setList(list);
        }

        @Override
        public void onFail(String msg) {

            Toast.makeText(context,msg, Toast.LENGTH_SHORT).show();
        }
    };





    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view= inflater.inflate(R.layout.fragment_train, container, false);
        trainName= (EditText) view.findViewById(R.id.editText_search_train);
        searchTrain=(ImageView)view.findViewById(R.id.imageView_searchtrain_button);
        searchTrain.setOnClickListener(this);
        TrainModel TrainModel =new TrainModel();
        TrainModel.getTrainList(trainListener);
        init();

        return view;

    }


    private void init() {
        context=this.getActivity();
        recyclerView= (RecyclerView) view.findViewById(R.id.recyclerView_train);
        layoutManager=new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        trainAdapter=new TrainAdapter(context);
        trainAdapter.setList(list);
        recyclerView.setAdapter(trainAdapter);
// 设置Item之间间隔样式
        recyclerView .addItemDecoration(new DividerItemDecoration(context,LinearLayoutManager.VERTICAL));

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageView_searchtrain_button:
                search();
                break;
        }
    }

    private void search() {
        String name = trainName.getText().toString();
        TrainSearchModel trainSearchModel = new TrainSearchModel();
        trainSearchModel.searchTrain(name, this);

    }

    @Override
    public void onSuccess(List<TrainBean> trainBean) {
        list=trainBean;
        trainAdapter.setList(list);
    }

    @Override
    public void onFail(String msg) {
        Toast.makeText(context,msg, Toast.LENGTH_SHORT).show();
    }
}